package com.ulook.lookplanet.ui.notifications.friend;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;

import android.content.Intent;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.qmuiteam.qmui.widget.QMUITopBarLayout;
import com.ulook.lookplanet.R;
import com.ulook.lookplanet.data.model.TUser;
import com.ulook.lookplanet.ui.notifications.chat.ChatActivity;
import com.ulook.lookplanet.widget.FriendView;

import java.util.List;

public class FriendActivity extends AppCompatActivity {

    private FriendViewModel viewModel;
    private LinearLayout friendLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_friend);

        viewModel = new ViewModelProvider(this).get(FriendViewModel.class);
        viewModel.getToastText().observe(this, s -> {
            Toast.makeText(FriendActivity.this, s, Toast.LENGTH_SHORT).show();
        });
        viewModel.getFriends().observe(this, tUsers -> {
            displayFriend(tUsers);
        });

        Intent intent = getIntent();
        int currUserId = intent.getIntExtra("currUserId", 0);
        if(currUserId == 0){
            Toast.makeText(this, "未登录", Toast.LENGTH_SHORT).show();
            finish();
        }
        viewModel.setCurrUserId(currUserId);

        initTopBar();
        initData();

    }

    private void initTopBar(){
        QMUITopBarLayout topBar = findViewById(R.id.friend_bar);
        topBar.setTitle("关注列表");
        topBar.addLeftBackImageButton().setOnClickListener(view -> {
            finish();
        });
    }

    private void initData(){
        friendLayout = findViewById(R.id.friend_layout);
        viewModel.getFriendList();
    }

    /**
     * 展示好友列表
     */
    private void displayFriend(List<TUser> users){
        for(int i = 0; i < users.size(); i++){
            FriendView friendView = new FriendView(this);
            friendView.setWidgetData(users.get(i), viewModel.getCurrUserId());
            friendLayout.addView(friendView);
        }
    }
}